<template>
	<view>
		<view class="block">
			<view class="content">
				<view class="orderinfo">
					<!-- <view class="row">
						<view class="nominal">openId:</view>
						<view class="text">{{ openId }}</view>
					</view> -->
					<!-- <view class="row">
						<view class="nominal">订单号:</view>
						<view class="text">{{ order_id || '' }}</view>
					</view> -->
					<view class="row">
						<view class="nominal">订单金额:</view>
						<view class="text">{{ price }}元</view>
					</view>
				</view>
			</view>
		</view>
		<view class="block">
			<view class="title">选择支付方式</view>
			<view class="content">
				<view class="pay-list">
					<view class="row" @tap="paytype = 'wxpay'">
						<view class="left"><image src="/static/img/wxpay.png"></image></view>
						<view class="center">微信支付</view>
						<view class="right"><radio :checked="paytype == 'wxpay'" color="#00bc0c" /></view>
					</view>
				</view>
			</view>
		</view>
		<!-- <view class="pay">
			<view class="btn" @tap="doDeposit">立即支付</view>
		</view> -->
	</view>
</template>

<script>
// import { getOpenId } from '@/common/utils/miniProgram.js';
// import { mpWxPay,hlbmpWxPay } from '../../common/apis/pay.js';
export default {
	data() {
		return {
			formData: {
				amount: 0,
				orderName: '',
				orderType: '',
				payType: '',
				isZero: '',
				password: '',
				token: '', // java的token
				memToken: '', // php的token
			},
			paytype: 'wxpay', //支付类型
			openId: '',
			price:100,
			order_id:'',
			type:1,//1：料支付，2：余额充值，
			uid:'',
			pay_channel_id:'',
		};
	},
	async onLoad(e) {
		console.log(e);
		if(e.price){
			this.price = e.price
		}
		if(e.order_id){
			this.order_id = e.order_id
		}
		if(e.type){
			this.type = e.type
		}
		if(e.uid){
			this.uid = e.uid
		}
		if(e.pay_channel_id){
			this.pay_channel_id = e.pay_channel_id
		}
		console.log('this.pay_channel_id: ',this.pay_channel_id);
		if(e.test){
			if(e.test = 'test'){
				uni.redirectTo({
				  url: '/pages/pay/success/success?amount=' + this.formData.amount + '&type=' + this.type,
				});
			}
		}
		this.doDeposit()
	},
	methods: {
		doDeposit() {
			
			let url
			if(this.type == 1){
				 url = 'https://api.gcsliao.com/index/Materialuser/pay'
			}else{
				url = 'https://api.gcsliao.com/index/User/recharge'
			}
			uni.request({
			    url: url, //仅为示例，并非真实接口地址。
			    data: {
			        text: 'uni.request'
			    },
				method:'post',
				data:{
					price:this.price,
					order_id:this.order_id,
					pay_channel:'wx_lite',
					login_type:'1',
					uid: this.uid,
					pay_channel_id:this.pay_channel_id
				},
			    header: {
			       'content-type':'application/x-www-form-urlencoded',
			    },
			    success: (res) => {
			        console.log(res.data);
					if(res.data.code == 0){
						this.mpWxPay(res.data.data)
					}else{
						uni.showToast({
							title:res.data.msg,
							icon:'none'
							})
					}
			        // this.text = 'request success';
			    }
			});
		},
		async getOpenId() {
			try {
				const idInfo = await getOpenId();
				console.log(idInfo);
				this.openId = idInfo.openid;
				// uni.showToast({
				// 	icon: 'none',
				// 	title: `获取到的openId为：${this.openId}`,
				// });
			} catch (e) {
				console.error(e);
				uni.showToast({
					icon: 'none',
					title: e,
				});
			}
		},
		async mpWxPay(payInfo) {
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: payInfo.timeStamp,
				nonceStr: payInfo.nonceStr,
				package: payInfo.package,
				signType: payInfo.signType,
				paySign: payInfo.paySign,
				success: () => {
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						title: '支付成功'
					});
					setTimeout(() => {
						// uni.navigateTo({
						//     url: 'yuliaozhizhongzhengzhou://'
						// });
						
						uni.redirectTo({
						  url: '/pages/pay/success/success?amount=' + this.price + '&type=' + this.type,
						});
					}, 700);
				},
				fail: (err) => {
					uni.hideLoading();
					console.log(err);
					uni.showToast({
						icon: 'none',
						title: '支付失败'
					});
				},
				complete: () => {
					uni.hideLoading();
				}
			});
			return [null, null];
		},
		
		async hlbmpWxPay(){
			console.log("11111");
			uni.showLoading({
				title: '支付中...'
			});
			const [err, res] = await hlbmpWxPay({
				header: {
					cookiew: this.formData.token,
					cookiew2: this.formData.memToken,
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				data: {
					amount:this.formData.amount,
					ordersn: this.formData.orderName,
					// ordertype: this.formData.orderType,
					paytype: this.formData.payType,
					// isZero: this.formData.isZero,
					// password: this.formData.password,
					open_id: this.openId,
				}
			});
			if (err !== null) {
				console.error(err);
				return [err, null];
			}
			console.log(res);
			console.log(res.data.status);
			console.log(res.data.payInfo);
			if (res.data.status !== '1') {
				console.log("我在啊");
				uni.hideLoading();
				uni.showToast( {icon: 'none', title: res.data.msg} );
				return [res.data.msg, null];
			}
			let payInfoStr = ''
			let payInfo
			try{
				payInfoStr = res.data.payInfo;
				payInfo = payInfoStr;
			}catch(e){
				console.log(2222);
				uni.showToast({
					icon: 'none',
					title: ''+JSON.stringify(res.data)
				});
				uni.hideLoading();
				return;
			}
			console.log("payInfo: ",payInfo);
			
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: payInfo.timeStamp,
				nonceStr: payInfo.nonceStr,
				package: payInfo.package,
				signType: payInfo.signType,
				paySign: payInfo.paySign,
				success: () => {
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						title: '支付成功'
					});
					setTimeout(() => {
						uni.redirectTo({
						  url: '/pages/pay/success/success?amount=' + this.formData.amount,
						});
					}, 700);
				},
				fail: (err) => {
					uni.hideLoading();
					console.log(err);
					uni.showToast({
						icon: 'none',
						title: '支付失败'
					});
				},
				complete: () => {
					uni.hideLoading();
				}
			});
			return [null, null];
		}
	}
};
</script>

<style lang="scss">
.block {
	width: 94%;
	padding: 0 3% 40upx 3%;
	.title {
		width: 100%;
		font-size: 34upx;
	}
	.content {
		.orderinfo {
			width: 100%;
			border-bottom: solid 1upx #eee;
			.row {
				width: 100%;
				height: 90upx;
				display: flex;
				align-items: center;
				.nominal {
					flex-shrink: 0;
					font-size: 46upx;
					color: #7d7d7d;
				}
				.text {
					width: 70%;
					margin-left: 10upx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					font-size: 46upx;
				}
			}
		}
		.pay-list {
			width: 100%;
			border-bottom: solid 1upx #eee;
			.row {
				width: 100%;
				height: 120upx;
				display: flex;
				align-items: center;
				.left {
					width: 100upx;
					flex-shrink: 0;
					display: flex;
					align-items: center;
					image {
						width: 80upx;
						height: 80upx;
					}
				}
				.center {
					width: 100%;
					font-size: 30upx;
				}
				.right {
					width: 100upx;
					flex-shrink: 0;
					display: flex;
					justify-content: flex-end;
				}
			}
		}
	}
}
.pay {
	margin-top: 20upx;
	width: 100%;
	display: flex;
	justify-content: center;
	flex-wrap: wrap;
	.btn {
		width: 70%;
		height: 80upx;
		border-radius: 80upx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
		background-color: #00bc0c;
		box-shadow: 0upx 5upx 10upx rgba(0, 0, 0, 0.2);
	}
	.tis {
		margin-top: 10upx;
		width: 100%;
		font-size: 24upx;
		display: flex;
		justify-content: center;
		align-items: baseline;
		color: #999;
		.terms {
			color: #5a9ef7;
		}
	}
}
</style>
